#include <stdlib.h>

#include "genserver.h"

#define PORT            (9734)
#define HEADRSIZE       (4)
#define EXPTMESSIZE     (10)

typedef struct application_t
{
    GenSv*      m_sv;
}appl;


/*********CallBackFunction************************************************/
/* 1st 4 bytes in each meassage*/
size_t Headrfunc(void* _buf)
{
    return *((size_t*) _buf);
}

void NewClient(int _idclient,void* _app)
{
    /*if (_IDclient == ((appl*)_app)->m_sv->*/
    printf("\nNewClientCALLBACK , id : %d",_idclient);
    fflush(stdout);
    return;
}

void NewMessage(int _IDclient,void* _app, void*  _buf,size_t _mesSize)
{
    printf("\n Message recieved from\
     client ID %d  : %s \n", _IDclient, (char*)_buf+4);
    return;
}

void NewExeption(int _IDclient,void* _app, void*  _buf,size_t _mesSize)
{
    printf("Exeption message arrived from client\
    ID %d was %s :\n",_IDclient, (char*)_buf+4);
}

void ClientDiscn(int _IDclient,void* _app,size_t _notreaded,size_t _notwrited)
{
    printf("\n Problem with client ID %d ,\
     only %d _not readed and %d not writed stransfered  \n", _IDclient,_notreaded,_notwrited);
     return;
}

void regression_test();

int  unitest_CreateandRun();
int  unitest_RessiveMessage();
int  unitest_RessiveMessageandDisconnectClient();
int  unitest_RessiveMessageandDisconnectClient();
int  unitest_RessMessDiscDropClient();

int main()
{
    regression_test();
    return 0;
}

void regression_test()
{
    unitest_CreateandRun();
}

int  unitest_CreateandRun()
{
        appl*     app=(appl*)malloc(sizeof(appl));

    GenSv*      sv = GsvServerNew(PORT,app,HEADRSIZE,
        Headrfunc,NewClient,NewMessage,ClientDiscn,
        EXPTMESSIZE,NewExeption);
        GsvServerRun(sv);
        return 0;
}

int  unitest_NewClient()
{
    appl*     app=(appl*)malloc(sizeof(appl));

    GenSv*      sv = GsvServerNew(PORT,app,HEADRSIZE,
        Headrfunc,NewClient,NewMessage,ClientDiscn,
        EXPTMESSIZE,NewExeption);
        return 0;
}

int  unitest_RessiveMessage()
{
    appl*     app=(appl*)malloc(sizeof(appl));

    GenSv*      sv = GsvServerNew(PORT,app,HEADRSIZE,
        Headrfunc,NewClient,NewMessage,ClientDiscn,
        EXPTMESSIZE,NewExeption);
    app->m_sv = sv;
    return 0;
}
